﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace Sio.Mdm.Client
{
    public static class Helper
    {
        public static string GetPropertyName(string name)
        {
            return name.Replace("get_", "").Replace("set_", "");
        }
        public static string GetMethodSignature(MethodInfo method)
        {
            return method.MethodHandle.Value.ToString();
            //StringBuilder signature = new StringBuilder();

            //// TODO: Add inheritance
            //signature.Append(method.Name);
            //signature.AppendFormat("_{0}", method.GetParameters().Length);

            //if (method.IsGenericMethod)
            //{
            //    signature.AppendFormat("_{0}", method.GetGenericArguments().Length);
            //    signature.AppendFormat("_{0}", method.GetGenericMethodDefinition().ReturnType);
            //}
            //else
            //{
            //    signature.AppendFormat("_{0}", method.ReturnType.Name);
            //}

            //return signature.ToString();
        }
        public static List<PropertyInfo> FlattenProperties(Type type)
        {
            return FlattenProperties(type, new List<PropertyInfo>());
        }
        public static List<MethodInfo> FlattenMethods(Type type)
        {
            return FlattenMethods(type, new List<MethodInfo>());
        }

        private static List<PropertyInfo> FlattenProperties(Type type, List<PropertyInfo> result)
        {
            result.AddRange(type.GetProperties());
            
            foreach (Type iface in type.GetInterfaces())
            {
                FlattenProperties(iface, result);
            }

            return result;
        }
        private static List<MethodInfo> FlattenMethods(Type type, List<MethodInfo> result)
        {
            result.AddRange(type.GetMethods());

            foreach (Type iface in type.GetInterfaces())
            {
                FlattenMethods(iface, result);
            }

            return result;
        }

        internal static string GetMethodName(string name)
        {
            return name.Replace("add_", "").Replace("remove_", "");
            
        }
    }
}
